var express = require('express');
var router = express.Router();
var fs = require('fs');

/* GET index page. */
router.get('/', function(req, res,next) {
  if(!req.session.user){
    res.redirect("/login");
  }
  else{
    res.render("home",{title:'Home'});
  }
});

/* GET login page. */
router.route("/login").get(function(req,res){
  /*if(req.ip != '123.124.17.226'){*/
  console.log(req.ip+'\t@\t'+(new Date()));
  /*}*/
  res.render("login",{title:'用户登录'});
});

/* GET home page. */
router.get("/realtime",function(req,res){
  console.log('realtime from '+req.session.user.name+' @'+(new Date()));
  var preall = require('child_process').spawn(
    'python',
    ['/root/test/modules/realtime/preall.py']
  );
  var output = '';
  preall.stdout.on('data', function(data){
    /*console.log(output)*/
    output += data;
  });
  preall.on('close', function(code){
    if(code !== 0){
      /*console.log('fuck')*/
      return res.send(500, code);
    }
    return res.send(output);
  })
});

// get his csv
router.get("/hit",function(req,res){
  var snum = req.param('snum');
  console.log('hit '+snum+' from '+req.session.user.name);
  file_path = '/root/test/his/all/'+snum+'.csv';
  fs.exists(file_path, function(exist){
    if(exist){
      fs.readFile(file_path, 'utf-8', function(err, data){
        res.send(data);
      });
    }
    else{
      res.send('');
    }
  })
});

// get his csv
router.get("/res",function(req,res){
  var name = req.param('name');
  /*console.log('hit '+snum+' from '+req.session.user.name);*/
  file_path = '/home/kvenux/results/'+name+'.csv';
  fs.exists(file_path, function(exist){
    if(exist){
      fs.readFile(file_path, 'utf-8', function(err, data){
        res.send(data);
      });
    }
    else{
      res.send('');
    }
  })
});

/* GET home page. */
router.get("/index_pre",function(req,res){
  /*var snum = req.param('snum');*/
  /*console.log('hit '+snum+' from '+req.session.user.name);*/
  /*file_path = '/root/test/R_packages/index_pre_result';*/
  file_path = '/root/test/download/index/result.csv';
  fs.exists(file_path, function(exist){
    if(exist){
      fs.readFile(file_path, 'utf-8', function(err, data){
        res.send(data);
      });
    }
    else{
      res.send('');
    }
  })
});

/* GET home page. */
router.get("/sta_res",function(req,res){
  var name = req.param('name');
  console.log(name)
  var preall = require('child_process').spawn(
    'python',
    ['/root/test/ml/statics/'+name+'.py']
  );
  var output = '';
  preall.stdout.on('data', function(data){
    /*console.log(output)*/
    output += data;
  });
  preall.on('close', function(code){
    if(code !== 0){
      /*console.log('fuck')*/
      return res.send(500, code);
    }
    return res.send(output);
  })
});


router.get("/home",function(req,res){
  if(!req.session.user){
    req.session.error = "请先登录"
    res.redirect("/login");
  }
  res.render("home",{title:'古典'});
});

router.get("/statics0",function(req,res){
  /*if(!req.session.user){*/
  /*req.session.error = "请先登录"*/
  /*res.redirect("/login");*/
  /*}*/
  console.log("statics")
  res.render("statics",{title:'古典'});
});

router.get("/statics_iver",function(req,res){
  if(!req.session.user){
    req.session.error = "请先登录"
    res.redirect("/login");
  }
  res.render("statics_iver",{title:'古典'});
});

router.get("/statics_anthony",function(req,res){
  if(!req.session.user){
    req.session.error = "请先登录"
    res.redirect("/login");
  }
  res.render("statics_anthony",{title:'古典'});
});

router.get("/statics_kobe",function(req,res){
  if(!req.session.user){
    req.session.error = "请先登录"
    res.redirect("/login");
  }
  res.render("statics_kobe",{title:'古典'});
});

router.get("/west_world",function(req,res){
  if(!req.session.user){
    req.session.error = "请先登录"
    res.redirect("/login");
  }
  res.render("west_world",{title:'新潮'});
});

router.get("/newtest",function(req,res){
  if(!req.session.user){
    req.session.error = "请先登录"
    res.redirect("/login");
  }
  res.render("new_test",{title:'新潮'});
});

router.get("/logout",function(req,res){
  req.session = null;
  req.error = null;
  res.redirect("/home");
});

router.route("/login").post(function(req,res){
  //get User info
  var User = global.dbHandel.getModel('user');
  var uname = req.body.uname;
  User.findOne({name:uname},function(err,doc){
    /*console.log(doc);*/
    if(err){
      res.sendStatus(500);
      /*console.log(err);*/
    }else if(!doc){
      req.session.error = '用户名不存在';
      res.sendStatus(404);
      //	res.redirect("/login");
    }else{
      if(req.body.upwd != doc.password){
        req.session.error = "密码错误";
        res.sendStatus(404);
        //	res.redirect("/login");
      }else{
        console.log('login success from '+uname);
        req.session.user = doc;
        res.sendStatus(200);
        /*res.redirect("/home");*/
      }
    }
  });
});

router.route("/video").get(function(req,res){
  res.render("video",{title:'video'});
});

router.route("/videojs").get(function(req,res){
  res.render("videojs",{title:'videojs'});
});

module.exports = router;
